﻿using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.MediaProcessing.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using Vitus.MediaProcessing.Models;
using Vitus.MediaProcessing.Services;
using Vitus.MediaProcessing.ViewModels;

namespace Vitus.MediaProcessing.Drivers
{
    public class MediaProfileAttachmentsPartDriver : ContentPartDriver<MediaProfileAttachmentsPart>
    {
        private readonly IContentManager contentManager;
        private readonly IMediaProfileAttachmentService attachmentService;

        public MediaProfileAttachmentsPartDriver(IContentManager contentManager, IMediaProfileAttachmentService attachmentService)
        {
            this.contentManager = contentManager;
            this.attachmentService = attachmentService;
        }

        protected override string Prefix { get { return "MediaProfileAttachments"; } }

        protected override DriverResult Display(MediaProfileAttachmentsPart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_MediaProfileAttachments",
                () => shapeHelper.Parts_MediaProfileAttachments(Attachments: part.MediaProfileAttachments));
        }

        protected override DriverResult Editor(MediaProfileAttachmentsPart part, dynamic shapeHelper)
        {
            var profiles = contentManager.List<ImageProfilePart>("ImageProfile");

            var viewModel = new MediaProfileAttachmentsPartViewModel
            {
                Attachments = part.MediaProfileAttachments,
                Profiles = profiles,
                SelectedAttachments = string.Join("|", part.MediaProfileAttachments.Select(a => a.MediaPath + ":" + a.ProfileName))
            };

            return ContentShape("Parts_MediaProfileAttachments_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/MediaProfileAttachments",
                    Model: viewModel,
                    Prefix: Prefix));
        }

        protected override DriverResult Editor(MediaProfileAttachmentsPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            var viewModel = new MediaProfileAttachmentsPartViewModel();

            updater.TryUpdateModel(viewModel, Prefix, new string[] { "SelectedAttachments" }, null);

            var newAttachments = new List<Tuple<string, string>>();

            if (!string.IsNullOrEmpty(viewModel.SelectedAttachments))
            {
                var idPairs = viewModel.SelectedAttachments.Split('|');

                foreach (var idPair in idPairs)
                {
                    var ids = idPair.Split(':');

                    string mediaPath = ids[0];
                    string profileName = ids[1];

                    newAttachments.Add(Tuple.Create(mediaPath, profileName));
                }
            }

            foreach (var newAttachment in newAttachments.Where(na => !part.MediaProfileAttachments.Any(a => na.Item1 == a.MediaPath && na.Item2 == a.ProfileName)))
            {
                attachmentService.AddMediaProfileAttachment(part.Id, newAttachment.Item1, newAttachment.Item2);
            }

            foreach (var removedAttachment in part.MediaProfileAttachments.Where(a => !newAttachments.Any(na => na.Item1 == a.MediaPath && na.Item2 == a.ProfileName)))
            {
                attachmentService.DeleteMediaProfileAttachment(removedAttachment.Id);
            }

            return Editor(part, shapeHelper);
        }
    }
}
